package com.cjst.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.pojo.sys.SysDict;
import com.cjst.service.sys.SysDictService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;


/**
 * 公共控制器类
 */
@Controller
@RequestMapping(value = "/sys/common/")
public class CommonController {

    @Autowired
    private SysDictService dictService;

    /**
     * 菜单排序
     */
    public static List<SysDict> sortListByNum(List<SysDict> lists) {
        for (int i = 0; i < lists.size(); i++) {
            for (int j = 0; j < lists.size(); j++) {
                if (Integer.parseInt(lists.get(i).getNum()) > Integer
                    .parseInt(lists.get(j).getNum())) {
                    SysDict temp = lists.get(i);
                    lists.set(i, lists.get(j));
                    lists.set(j, temp);
                }
            }
        }
        return lists;
    }

    /**
     * 获取菜单列表
     */
    @RequestMapping(value = "/getDicts", method = RequestMethod.POST)
    @ResponseBody
    public Object getDicts(String type) {
        WherePrams prams = Method.where("delFlag", C.EQ, "0");
        prams.and("type", C.EQ, type);
        prams.and("pid", C.NE, "0");
        List<SysDict> dicts = dictService.list(prams);
        if (dicts != null && dicts.size() > 0) {
            dicts = sortListByNum(dicts);
        }
        return dicts;
    }
}
